<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "gky_we_fans".
 *
 * @property string $id
 * @property integer $weid
 * @property string $wename
 * @property string $openid
 * @property string $nickname
 * @property string $sex
 * @property string $language
 * @property string $city
 * @property string $province
 * @property string $country
 * @property string $headimgurl
 * @property string $subscribe_time
 * @property string $unionid
 * @property string $remark
 * @property string $groupid
 * @property integer $valid
 * @property string $unsubscribtime
 */
class GkyWeFans extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_we_fans';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['weid', 'valid', 'unsubscribtime', 'groupid'], 'integer'],
            [['subscribe_time'], 'safe'],
            [['wename', 'nickname'], 'string', 'max' => 128],
            [['openid', 'unionid'], 'string', 'max' => 256],
            [['sex'], 'string', 'max' => 8],
            [['language', 'province', 'country'], 'string', 'max' => 32],
            [['city'], 'string', 'max' => 64],
            [['headimgurl', 'remark'], 'string', 'max' => 512]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'weid' => '微信ID',
            'wename' => '微信号',
            'openid' => 'Openid',
            'nickname' => '昵称',
            'sex' => '性别',
            'language' => '语言',
            'city' => '城市',
            'province' => '省份',
            'country' => '国家',
            'headimgurl' => '头像URL',
            'subscribe_time' => '关注时间',
            'unionid' => 'Unionid',
            'remark' => '备注',
            'groupid' => 'Groupid',
            'valid' => 'Valid',
            'unsubscribtime' => '取消关注时间',
        ];
    }

    /**
     * 保存从微信获取的数据
     *
     * {openid,nickname,sex,province,city,country,headimgurl,privilege,[unionid]}
     *
     * @param $userInfo
     */
    public static function saveWithWeixin($userInfo){
        $user = GkyWeFans::find()->where(['openid'=>$userInfo['openid']])->one();
        if($user == null){
            $user = new GkyWeFans();
        }
        $user->openid = $userInfo['openid'];
        $user->nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '匿名';
        $user->sex = isset($userInfo['sex']) ? $userInfo['sex'] : '1';
        $user->province = isset($userInfo['province']) ? $userInfo['province'] : '';
        $user->city = isset($userInfo['city']) ? $userInfo['city'] : '';
        $user->country = isset($userInfo['country']) ? $userInfo['country'] : '';
        $user->headimgurl = isset($userInfo['headimgurl']) ? $userInfo['headimgurl'] : '';
        $user->unionid = isset($userInfo['unionid']) ? $userInfo['unionid'] : '';
        $user->save();
        return GkyWeFans::find()->where(['openid'=>$userInfo['openid']])->one();
    }
}
